{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "### 字典及其运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{} <class 'dict'>\n",
      "{'id': 1, 'name': 'Tome'} <class 'dict'>\n",
      "{'id': None, 'name': None, 'gender': None, 'salary': None} <class 'dict'>\n",
      "{'id': 'v1', 'name': 'v1', 'gender': 'v1', 'salary': 'v1'} <class 'dict'>\n",
      "{'id': 1, 'name': 'Tom', 'gender': 'male', 'salary': 1000} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 初始化空字典\n",
    "d1 = {}\n",
    "print(d1, type(d1))\n",
    "\n",
    "# 使用下标方式创建字典\n",
    "d1[\"id\"] = 1\n",
    "d1[\"name\"] = \"Tome\"\n",
    "print(d1, type(d1))\n",
    "\n",
    "# 从列表创建字典\n",
    "lst = [\"id\", \"name\", \"gender\", \"salary\"]\n",
    "d1 = {}.fromkeys(lst)\n",
    "print(d1, type(d1))\n",
    "\n",
    "# 从列表创建字典，并指定一个缺省的属性值\n",
    "lst = [\"id\", \"name\", \"gender\", \"salary\"]\n",
    "d1 = {}.fromkeys(lst, \"v1\")\n",
    "print(d1, type(d1))\n",
    "\n",
    "# 初始化字典\n",
    "d1 = {\n",
    "    \"id\": 1,\n",
    "    \"name\": \"Tom\",\n",
    "    \"gender\": \"male\",\n",
    "    \"salary\": 1000\n",
    "}\n",
    "print(d1, type(d1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'id': 1, 'name': 'Tom', 'gender': 'male', 'salary': 2000, 'extraHours': 8, 'hourlyRate': 30} <class 'dict'>\n",
      "{'id': 1, 'name': 'Tom', 'salary': 2000, 'extraHours': 8, 'hourlyRate': 30} <class 'dict'>\n",
      "{'id': 1, 'name': 'Tom', 'salary': 2000, 'extraHours': 8} <class 'dict'>\n",
      "{'id': 1, 'name': 'Tom', 'salary': 2000} <class 'dict'>\n",
      "{'id': 1, 'name': 'Tom'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 从字典中删除键及其键值\n",
    "emp1 = {\n",
    "    \"id\": 1,\n",
    "    \"name\": \"Tom\",\n",
    "    \"gender\": \"male\",\n",
    "    \"salary\": 2000,\n",
    "    \"extraHours\": 8,\n",
    "    \"hourlyRate\": 30\n",
    "}\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 使用pop方法删除指定的属性\n",
    "emp1.pop(\"gender\")\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 使用pop方法删除指定的属性\n",
    "emp1.pop(\"hourlyRate\")\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 使用popitem方法删除字典最末尾属性\n",
    "emp1.popitem()\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 使用del方法删除字典的某个属性\n",
    "del emp1[\"salary\"]\n",
    "print(emp1, type(emp1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'id': 1, 'name': 'Tom', 'gender': 'male', 'salary': 2000, 'extraHours': 8, 'hourlyRate': 30} <class 'dict'>\n",
      "1\n",
      "Tom\n"
     ]
    }
   ],
   "source": [
    "# 从字典中获取某个属性的值\n",
    "emp1 = {\n",
    "    \"id\": 1,\n",
    "    \"name\": \"Tom\",\n",
    "    \"gender\": \"male\",\n",
    "    \"salary\": 2000,\n",
    "    \"extraHours\": 8,\n",
    "    \"hourlyRate\": 30\n",
    "}\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 以下标方式获取id\n",
    "print(emp1[\"id\"])\n",
    "\n",
    "# 以get方法获取name\n",
    "print(emp1.get(\"name\"))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'id': 1, 'name': 'Tom', 'gender': 'male', 'salary': 2000, 'extraHours': 8, 'hourlyRate': 30} <class 'dict'>\n",
      "{'id': 1, 'name': 'Jerry', 'gender': 'male', 'salary': 2200, 'extraHours': 8, 'hourlyRate': 30} <class 'dict'>\n",
      "{'id': 1, 'name': 'Cherry', 'gender': 'male', 'salary': 2200, 'extraHours': 8, 'hourlyRate': 30} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 更新字典中的属性值\n",
    "emp1 = {\n",
    "    \"id\": 1,\n",
    "    \"name\": \"Tom\",\n",
    "    \"gender\": \"male\",\n",
    "    \"salary\": 2000,\n",
    "    \"extraHours\": 8,\n",
    "    \"hourlyRate\": 30\n",
    "}\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 直接属性值更新\n",
    "emp1[\"name\"] = \"Jerry\"\n",
    "emp1[\"salary\"] = 2200\n",
    "print(emp1, type(emp1))\n",
    "\n",
    "# 使用update方法更新\n",
    "emp1.update(name=\"Cherry\", salary=2200)\n",
    "print(emp1, type(emp1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['id', 'name', 'gender', 'salary', 'extraHours', 'hourlyRate']) <class 'dict_keys'>\n"
     ]
    }
   ],
   "source": [
    "# 获取字典的键值\n",
    "d1 = emp1.keys()\n",
    "print(d1, type(d1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['id', 'name'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 转换字典的键值为列表\n",
    "l1 = list(emp1.keys())\n",
    "\n",
    "print(l1, type(l1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id\n",
      "name\n",
      "gender\n",
      "salary\n",
      "extraHours\n",
      "hourlyRate\n"
     ]
    }
   ],
   "source": [
    "# 以字典的keys进行迭代\n",
    "for x in emp1.keys():\n",
    "    print(x)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values([1, 'Tom', 'male', 2000, 8, 30]) <class 'dict_values'>\n"
     ]
    }
   ],
   "source": [
    "# 获取字典的values\n",
    "v1 = emp1.values()\n",
    "print(v1, type(v1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 'Tom', 'male', 2000, 8, 30] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 把字典values转成列表\n",
    "l1 = list(emp1.values())\n",
    "print(l1, type(l1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "Tom\n",
      "male\n",
      "2000\n",
      "8\n",
      "30\n"
     ]
    }
   ],
   "source": [
    "# 以字典的值做迭代\n",
    "for x in emp1.values():\n",
    "    print(x)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}